//package com.yisu.es;
//
//import com.yisu.es.executor.EsQueryExecutor;
//import com.yisu.es.model.EsQueries;
//import com.yisu.es.parser.EsQueryParser;
//import org.apache.http.HttpHost;
//import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.search.SearchHit;
//
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Map;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//public class Test {
//    private static final Logger logger = LoggerFactory.getLogger(Test.class);
//
//    public static void main(String[] args) {
//        // 外部化配置
//        String host = System.getProperty("elasticsearch.host", "localhost");
//        int port;
//        try {
//            port = Integer.parseInt(System.getProperty("elasticsearch.port", "9200"));
//        } catch (NumberFormatException e) {
//            logger.error("Invalid port number: {}", e.getMessage());
//            return;
//        }
//        String scheme = System.getProperty("elasticsearch.scheme", "http");
//
//        // XML 文件路径外部化
//        String queryFilePath = System.getProperty("es.query.file.path", "/mapper/es-queries.xml");
//
//        // 使用 try-with-resources 自动管理资源
//        try (RestHighLevelClient client = new RestHighLevelClient(
//                RestClient.builder(new HttpHost(host, port, scheme)))) {
//
//            try {
//                EsQueries esQueries = EsQueryParser.parse(queryFilePath);
//                EsQueryExecutor executor = new EsQueryExecutor(client, esQueries);
//
//                Map<String, Object> params = new HashMap<>();
//                // 输入验证
//                String userName = validateUserName("张三");
//                params.put("userName", userName);
//
//                // 调试：打印实际查询字符串
////                String actualQuery = executor.executeQuery("findByName", params);
////                logger.info("Executing query: {}", actualQuery);
//
//                SearchResponse response = executor.executeQuery("findByName", params);
////                SearchResponse response =  executor.executeExactMatchQuery();
//                for (SearchHit hit : response.getHits().getHits()) {
//                    System.out.println(hit.getSourceAsString());
//                    logger.info("Successfully retrieved document: {}", hit.getId());
//                }
//                logger.info("Query executed successfully.");
//
//            } catch (Exception e) {
//                logger.error("Error executing query: {}", e.getMessage(), e);
//                // 打印堆栈跟踪以帮助调试
//                e.printStackTrace();
//            }
//
//        } catch (IOException e) {
//            logger.error("Error closing client: {}", e.getMessage(), e);
//        }
//    }
//
//    // 输入验证示例方法
//    private static String validateUserName(String userName) {
//        if (userName == null || userName.isEmpty()) {
//            throw new IllegalArgumentException("User name cannot be null or empty");
//        }
//        // 可以添加更多验证逻辑
//        return userName;
//    }
//}
